/*
 * Title:        CloudSim Toolkit
 * Description:  CloudSim (Cloud Simulation) Toolkit for Modeling and Simulation of Clouds
 * Licence:      GPL - http://www.gnu.org/copyleft/gpl.html
 *
 * Copyright (c) 2009-2010, The University of Melbourne, Australia
 */
package org.cloudbus.cloudsim.power.models.scheduling;

import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.cloudbus.cloudsim.Cloudlet;
import org.cloudbus.cloudsim.Log;
import org.cloudbus.cloudsim.Vm;

/**
 * Class responsable to coordinate a specific class (FrontEnd + LocalDatacenter + Estimator)
 * 
 * @author Márcio Costa Junior - University of Sao Paulo
 */
public class ClassMid {
    
    private int id;
    private int midId;
    private double maxBarrier;
    private double mipsBarrier;
    
    private FrontEnd frontEnd;
    private LocalDatacenter datacenter;
    private Estimator estimator;
    
    public ClassMid(double mipsBarrier, double maxBarrier, int id, int midId){
            setId(id);
            setMaxBarrier(maxBarrier);
            setMipsBarrier(mipsBarrier);
            this.midId = midId;
    }
    
    public void createFrontEnd(int numberHosts, int numberPe){
            try {
                frontEnd = new FrontEnd("FrontEnd_" + getId() + "_" + midId, getId());
                getFrontEnd().setNumberHosts(numberHosts);
                getFrontEnd().setNumberPe(numberPe);
            } 
            catch (Exception ex) {
                Logger.getLogger(ClassMid.class.getName()).log(Level.SEVERE, null, ex);
                Log.printLine("It's not possible to create a front end at the class number " + getId());
            }

        
    }
    
    public void createLocalDatacenter(int mips, int ram, long storage, int bw, double timeZone, double peCost, 
            long costPerMem, long costPerStorage, long costPerBw, double pePotency, double powerPercent,
            double processingCost, String os, String arch, String vmm){
        
               
            try {
                getFrontEnd().setHostsCaracteristics(mips, ram, storage, bw, timeZone, peCost, costPerMem, 
                            costPerStorage, costPerBw, pePotency, powerPercent, processingCost, os, arch, vmm);
                getFrontEnd().createLocalDatacenter("LocalDatacenter_" + getId() + "_" + midId, getFrontEnd().getNumberHosts()*getId());
                getFrontEnd().getLocalDatacenter().setDisableMigrations(true);
                for(int i = 0; i < getFrontEnd().getNumberHosts(); i++){
                            getFrontEnd().getLocalDatacenter().changeStatus((MoreRealisticHost) getFrontEnd().getLocalDatacenter().getHostList().get(i), MoreRealisticHost.TURNED_ON);
                        }
            } 
            catch (Exception ex) {
                Logger.getLogger(ClassMid.class.getName()).log(Level.SEVERE, null, ex);
                Log.printLine("It's not possible to create a local datacenter at the class number " + getId());
            }
                
        
    }
    
    public void submitRequestList(List<Vm> vmList, List<Cloudlet> cloudletList){
        
		getFrontEnd().submitVmList(vmList);
		getFrontEnd().submitCloudletList(cloudletList);
    }
    
    public void createEstimator(int requests){
            estimator = new Estimator("Estimator_" + getId() + "_" + midId, requests, getFrontEnd());
    }
    
    public void sendRequesttoMiddleware(){
        
    } 

    /**
     * @return the mipsBarrier
     */
    public double getMipsBarrier() {
        return mipsBarrier;
    }

    /**
     * @param mipsBarrier the mipsBarrier to set
     */
    public void setMipsBarrier(double mipsBarrier) {
        this.mipsBarrier = mipsBarrier;
    }

    /**
     * @return the maxBarrier
     */
    public double getMaxBarrier() {
        return maxBarrier;
    }

    /**
     * @param maxBarrier the maxBarrier to set
     */
    public void setMaxBarrier(double maxBarrier) {
        this.maxBarrier = maxBarrier;
    }

    /**
     * @return the id
     */
    public int getId() {
        return id;
    }

    /**
     * @param id the id to set
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * @return the frontEnd
     */
    public FrontEnd getFrontEnd() {
        return frontEnd;
    }

    /**
     * @return the datacenter
     */
    public LocalDatacenter getDatacenter() {
        return datacenter;
    }

    /**
     * @return the estimator
     */
    public Estimator getEstimator() {
        return estimator;
    }
    
}
